package com.zh.freechat.infrastructure.mq;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import com.zh.freechat.domain.notice.NoticeMessage;
import com.zh.webcommon.common.support.observer.DataObserver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executors;

/**
 * SSE消息MQ生产者
 * @author ZH
 * @date 14:10 2020/8/19
 */
@Slf4j
@Component
public class SseMsgMQProducer {
    private final EventBus eventBusAsync = new AsyncEventBus(Executors.newSingleThreadExecutor());


    /**
     * 发送SSE消息到MQ
     * @param sseMsg
     */
    public void sendMsg(NoticeMessage sseMsg) {
        sseMsg.setStale(true);

        eventBusAsync.post(sseMsg);

        log.debug("MQ 发送SseMsg={} 的消息",sseMsg);
    }

    /**
     * 注册消费者
     * @param observer
     */
    public void register(DataObserver<?> observer) {
        this.eventBusAsync.register(observer);
    }
}
